package com.example.music;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.music.sql.DBOpenHelper;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private DBOpenHelper mDBOpenHelper;
    private Button re_register;
    private ImageView re_return;
    private EditText reg_uname,reg_pwd,reg_pwd2;
    private String uname,pwd,pwd2;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();
        mDBOpenHelper = new DBOpenHelper(this);
    }

    private void initView() {
        reg_uname=(EditText) findViewById(R.id.reg_uname);
        reg_pwd=(EditText) findViewById(R.id.reg_pwd);
        reg_pwd2=(EditText) findViewById(R.id.reg_pwd2);
        re_register=(Button) findViewById(R.id.re_register);
        re_return=(ImageView) findViewById(R.id.re_return);

        re_register.setOnClickListener(this);
        re_return.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //返回登录界面
            case R.id.re_return:
                Intent intent= new Intent(this, LoginActivity.class);
                startActivity(intent);
                finish();
                break;
            //注册按钮
            case R.id.re_register:
            //获取用户输入的用户名、密码
                uname=reg_uname.getText().toString().trim();
                pwd=reg_pwd.getText().toString().trim();
                pwd2=reg_pwd2.getText().toString().trim();

                //判断
                if((uname.equals("") || pwd.equals(""))){
                    Toast.makeText(RegisterActivity.this,"用户名或密码不能为空",Toast.LENGTH_SHORT).show();
                }else if (!(pwd.equals(pwd2))){
                    Toast.makeText(RegisterActivity.this,"两次密码输入不一致",Toast.LENGTH_SHORT).show();
                }else {
                    //loginInfo表示文件名
                    sp=getSharedPreferences("loginInfo",MODE_PRIVATE);
                    //获取编辑器
                    editor=sp.edit();
                    //存入注册的用户信息
                    editor.putString("username",uname);
                    editor.putString("password",pwd);
                    //提交修改
                    editor.commit();
                    Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getApplicationContext(),LoginActivity.class));
                }
                break;

        }

    }
}